#source E:/eclipse/workspace/tayel-scripts-open-space/Tcl/UsefulProcs.tcl

proc draft001 {} {
    set fh [open U:/temp/report.txt w]
    set fileList [glob -nocomplain -directory {P:\03_Integration\ComStack Builder Tool\02_Development\01_EPC_DataModels} Lin*]
    foreach file_path $fileList {
        set file_name [file tail $file_path]
        set project_file [file join {e:\documents and settings\MTayel\My Documents\CSProjects\GUIStartup\GUIStartup\EPCModels} $file_name]
        if {[file mtime "$file_path"] > [file mtime "$project_file"]} {
            #exec {C:\Program Files\WinMerge\WinMergeU.exe} /x "$file_path" "$project_file" &
            puts $fh $file_name
        }
    }
    close $fh
}


proc draft002 {} {
    set fh [open U:/temp/PVCSHashes.txt]
    set dump [read $fh]
    set lines [split $dump \n]
    foreach line $lines {
        set pair [split $line ,]
        set Hashes([file tail [lindex $pair 0]]) [lindex $pair 1]
    }
    close $fh
    
    set fh [open U:/temp/ProjectHashes.txt]
    set dump [read $fh]
    set lines [split $dump \n]
    foreach line $lines {
        set pair [split $line ,]
        set PHashes([file tail [lindex $pair 0]]) [lindex $pair 1]
    }
    close $fh
    
    set fh [open U:/temp/report.txt w]
    foreach name [array names Hashes] {
        if {![string equal $PHashes($name) $Hashes($name)]} {
            set file_path [file join {P:\03_Integration\ComStack Builder Tool\02_Development\01_EPC_DataModels} $name]
            set project_file [file join {U:\CSProjects\GUIStartup\GUIStartup\EPCModels} $name]
            puts $fh "exec {C:/Program Files/WinMerge/WinMergeU.exe} /x \"$file_path\" \"$project_file\" &"
        }
    }
    
    close $fh
}

proc draft003 {{input_file U:/temp/input.txt}} {
    set fh [open $input_file]
    set dump [read $fh]
    close $fh
    set lines [split $dump \n]
    foreach line $lines {
        if {$line == ""} {
            lappend items $concated_lines
            set concated_lines {}
        } else {
            append concated_lines $line\n
        }
    }
    foreach item $items {
        regexp {([^\s]+)\s([^\n]+)(\s.+\n)(.+)} $item dummy name range dummy desc
        puts $item
        puts name:$name
        puts range:$range
        puts desc:$desc
        puts ##############################
    }
}

proc print80chars {input_string {indentation_length 0}} {
    set indentation [string repeat " " $indentation_length]
    if {(([string length $input_string] + $indentation_length) >= 78)} {
    }
}
proc GlobRecursed {directory pattern} {
    lappend globbed {*}[glob -nocomplain -directory $directory -type f $pattern]
    foreach dir [glob -nocomplain -directory $directory -type d *] {
        lappend globbed {*}[GlobRecursed $dir $pattern]
    }
    return $globbed
}
proc createFileStruct {} {
    package require fileutil
    set globbed [GlobRecursed {P:\02_Modules\04_ComM\02_Development\02_Source_Code} *ComM*{.c,.h}] 
    foreach filename $globbed {
        regsub -all {04_ComM} $filename {05_BswM} filepath
        regsub -all ComM $filepath BswM filepath
        set filepath [file normalize $filepath]
        ::fileutil::touch -f $filepath
    }
    package forget fileutil
}

proc createServiceIDDefines {{CDDDumpPath U:/temp/CDDDump.txt} {CompPrefix BswM}} {
    set fh [open $CDDDumpPath]
    set dump [read $fh]
    close $fh
    set lines [split $dump \n]
    foreach line $lines {
        incr pos
        if {[string match "Service Name:" $line]} {
            lappend array_list [lindex $lines $pos]
        } elseif {[regexp {Service ID \(hex\)\:\s*([0-9xA-F]*)} $line dummy ServiceID]} {
            lappend array_list $ServiceID
        }
    }
    array set services $array_list
    foreach name [array names services] {
        regexp $CompPrefix\_\(.+\) $name dummy service
        regsub -all {([^_])([A-Z])([^_A-Z])} $service {\1_\2\3} fixed
        regexp {(.{1,21}).*} $fixed dummy truncated
        set capitalized [string toupper $truncated]
        if {$services($name) ne {}} {
            puts [format "%-*s %s" 36 "#define $CompPrefix\_u8$capitalized" $services($name)]
        }
    }
}

proc findInDirectory {regex_string folder_path {output_report ""}} {
    set file_found false
    if {[string equal $output_report ""]} {
        set fh stdout
    } elseif {[catch {set fh [open $output_report w]}]} {
        puts "couldn't open \"$output_report\""
        puts "output will be displayed in console"
        puts #####################################
        set fh stdout
    } else {
        set file_found true
    }
    package require fileutil
    set lines [::fileutil::grep $regex_string [GlobRecursed $folder_path *]]
    foreach line $lines {
        regexp {((?:[A-Za-z]:)?.*?:\d+:)(.+)} $line dummy line_1 line_2
        puts $fh $line_1
        puts $fh "$line_2\n"
    }
    if {$file_found} {
        close $fh
    }
}

proc createFileInclDot {directory {execlude_stubs false} {stub_filter 04_Stubs} {output_path U:/temp/output} {format gif}} {
    package require fileutil
    #getting file list
    set file_list [GlobRecursed $directory *{.c,.h}]
    if {![regexp "\\.$format" $output_path]} {
        set output_path $output_path.$format
    }
    set created false
    set tf_name ""
    # getting a temp file name and opening it
    while {!$created} {
        set randn [string map {. {}} [expr rand()]]
        set tf_name [file normalize [file join $::env(TEMP) $randn.dot]]
        if {[file exists $tf_name]} {
            continue
        }
        set created true
        puts $tf_name
    }
    set fh [open $tf_name w]
    puts $fh "digraph graphname \{"
    puts "digraph graphname \{"
    puts $fh {    size="7.5,10"}
    puts $fh {    ratio=fill}
    puts $fh {    edge [arrowsize=2 weight=1.5]}
    puts $fh {    node [shape=box style=filled fontname=Tahoma height=1.0 fontsize=40]}
    #looping on files
    if {$execlude_stubs} {
        foreach file_path $file_list {
            set file_path [file normalize $file_path]
            set file_name [file tail $file_path]
            set node_name [string tolower $file_name]
            if {($stub_filter ne "") && [regexp $stub_filter $file_path]} {
                set fillcolor grey
                set stubattr($node_name) "    \"$node_name\" \[label=\"$file_name\", fillcolor=$fillcolor\]"
            }
        }
    }
    foreach file_path $file_list {
        set file_path [file normalize $file_path]
        set file_name [file tail $file_path]
        set node_name [string tolower $file_name]
        if {$node_name eq "memmap.h"} {
            continue
        }
        set fillcolor "\"#C5C5FF\""
        if {($stub_filter ne "") && [regexp $stub_filter $file_path]} {
            set fillcolor grey
            if {$execlude_stubs} {
                continue
            }
        } elseif {[regexp {\.h$} $file_path]} {
            set fillcolor "\"#C5FFC5\""
        }
        puts $fh "    \"$node_name\" \[label=\"$file_name\", fillcolor=$fillcolor\]"
        set lines [::fileutil::grep {^\s*#include\s*["<].+?[">]\s*$} $file_path]
        foreach line $lines {
            if {[regexp {#include\s*["<](.+?)[">]} $line dummy included_file]} {
                set lower [string tolower $included_file]
                if {($lower ne "memmap.h") && \
                    ((![info exists included_list]) || ($lower ni $included_list))} {
                    lappend included_list $lower
                    if {($execlude_stubs) && ($lower in [array names stubattr])} {
                        puts $fh $stubattr($lower)
                        array unset stubattr $lower
                    }
                    puts $fh "    \"$node_name\" -> \"$lower\";"
                }
            }
        }
        if {[info exists included_list]} {
            unset included_list
        }
    }
    puts $fh \}
    puts \}
    close $fh
}
proc validateIP {IP} {
    if {[regexp {^(?:(\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))(?:\.((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))){3}$} $IP]} {
        puts "$IP is a valid IP"
    } else {
        puts "INVALID-ADDRESS"
    }
}
####Usage Example
##findInDirectory {.*\r\n#include "[Mm]em[Mm]ap.h"} {P:\02_Modules\04_ComM\02_Development\02_Source_Code} U:/temp/output